package mi.payment.transaction.sale;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import lainey.ui.utils.LogUtil;
import lainey.ui.widget.TitleView;
import mi.payment.Constant;
import mi.payment.R;
import mi.payment.constants.Configs;
import mi.payment.constants.Params;
import mi.payment.settings.TransactionSwitchOperation;
import mi.payment.transaction.BaseMenuActivity;
import mi.payment.transaction.PayModeView;
import mi.payment.transaction.authorize.base.PreAuthorizeMainActivity;
import mi.payment.ui.base.AmountInputView;
import mi.payment.ui.base.NumberKeyboard;
import mi.payment.ui.calculator.CalculatorActivity;
import mi.payment.ui.other.LoginActivity;
import mi.payment.utils.SettingHomePageUtils;
import mi.payment.utils.transaction.ParamsInitTask;

/**
 * 消费模块 -- 首页
 */
public class HomeInputMoneyActivity extends BaseMenuActivity {

    private static final int REQUEST_CODE = 1;

    private PayModeView mPayModeView;
    private AmountInputView mAmountInputView;

    private boolean isCalculatorAmount = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();

        // 是否去其他首页
        boolean goActivity = goHomeActivity();
        if (goActivity) return;

        ParamsInitTask.isChecked = true;

        if (ParamsInitTask.isChecked) {
            SettingHomePageUtils.getInstance().setHomeActivity(this);
            return;
        }

        // 是否需要自动签到
        boolean isAuto = TransactionSwitchOperation.getInstance().getTransactionSwitchSetting().isAutoChecked();
        if (isAuto) {
            // mParamsCheckExt.autoLoginAndSign();
        } else {
            openActivity(LoginActivity.class);
        }


    }

    private boolean goHomeActivity() {
        SettingHomePageUtils.SettingHome settingHome = SettingHomePageUtils.getInstance().getSettingHome();
        if (settingHome == null) return false;
        LogUtil.e(Constant.TAG, "settingHome:" + settingHome.nowHome);
        if (settingHome.nowHome.equals("PreAuthorizeMainActivity") && SettingHomePageUtils.goPre) {
            SettingHomePageUtils.goPre = false;
            openActivity(PreAuthorizeMainActivity.class);
            finish();
            return true;
        }
        return false;
    }

    @Override
    public void sendCloseAppBroadcast() {
        super.sendCloseAppBroadcast();
        SettingHomePageUtils.goPre = true;
    }

    @Override
    public View getView() {
        return View.inflate(this, R.layout.activity_home_input_money, null);
    }

    @Override
    public void onLogonSuccess() {
        if (Configs.isClosePreAuth) {
            LogUtil.e(Constant.TAG, "关闭预授权界面");
        } else {
            SettingHomePageUtils.getInstance().init(this);
            SettingHomePageUtils.getInstance().setHomeActivity(this);
        }
    }

    private void initView() {
        TitleView titleView = findViewById(R.id.title_view);

        titleView.setCenterText(R.string.menu_sale);
        titleView.setLeftImageResource(R.drawable.ic_menu);

        Drawable drawable = getDrawable(R.drawable.ic_pre_auth);
        int minimumWidth = drawable.getMinimumWidth();
        int minimumHeight = drawable.getMinimumHeight();
        drawable.setBounds(0, 0, minimumWidth, minimumHeight);

        TextView rightText = titleView.getRightText();
        rightText.setText(R.string.menu_auth);
        rightText.setCompoundDrawablePadding(8);
        rightText.setCompoundDrawables(drawable, null, null, null);

        titleView.setLeftImageOnClickListener(
                v -> openMenu()
        );
        rightText.setOnClickListener(
                v -> {
                    openActivity(PreAuthorizeMainActivity.class);
                    finish();
                }
        );

        // 是否关闭预授权整个模块
        if (Configs.isClosePreAuth) {
            rightText.setVisibility(View.GONE);
        } else {
            rightText.setVisibility(View.VISIBLE);
        }

        // 计算收款金额
        findViewById(R.id.item_calculator).setOnClickListener(this);

        mAmountInputView = findViewById(R.id.amountInputView);
        mPayModeView = findViewById(R.id.payModeView);

        mPayModeView.setParamsCheckExt(mParamsCheckExt);
        mPayModeView.setAmountInputView(mAmountInputView);

        NumberKeyboard numberKeyboard = findViewById(R.id.numberKeyboard);
        numberKeyboard.setKeyClickCallback(
                new NumberKeyboard.KeyClickCallback() {

                    @Override
                    public void onNumClick(int keyNum) {
                        cleanCalculatorAmount();
                        mAmountInputView.addText(keyNum + "");
                    }

                    @Override
                    public void onDelClick() {
                        // 如果点击过小数点  再点击小数点 就不再操作
                        boolean isClickDot = mAmountInputView.isClickDot();
                        if (isClickDot) return;

                        cleanCalculatorAmount();
                        if (Configs.isConfigKeyboardDot) {
                            mAmountInputView.setClickDot(true);
                            mAmountInputView.setRightIndex(0);
                        } else {
                            mAmountInputView.delLast();
                        }
                    }

                    @Override
                    public void onCleanClick() {
                        cleanCalculatorAmount();
                        if (Configs.isConfigKeyboardDot) {
                            mAmountInputView.delLast();
                        } else {
                            mAmountInputView.clean();
                        }
                    }

                }
        );

    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        switch (id) {
            case R.id.item_calculator:
                Intent intent = new Intent(this, CalculatorActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
                break;
            default:
                break;
        }
    }

    private void cleanCalculatorAmount() {
        if (isCalculatorAmount) {
            isCalculatorAmount = false;
            mAmountInputView.clean();
            mAmountInputView.setClickDot(false);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && data != null) {
            final String result = data.getStringExtra("result");
            mAmountInputView.clean();
            mAmountInputView.setClickDot(false);
            char[] chars = result.toCharArray();
            for (char ch : chars) {
                boolean equals = TextUtils.equals(ch + "", ".");
                if (equals) {
                    mAmountInputView.setClickDot(true);
                    mAmountInputView.setRightIndex(0);
                } else {
                    mAmountInputView.addText(ch + "");
                }
            }
            isCalculatorAmount = true;
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mPayModeView != null) {
            mPayModeView.initView();
        }

        if (mAmountInputView != null) {
            mAmountInputView.clean();
        }
        Params.resetTradeData();
    }

    public static void reStart(Context context) {
        Intent intent = new Intent(context, HomeInputMoneyActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

}
